<?php
/**

*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	public function __construct($application)
	{
		parent::__construct($application);

		Zend_Registry::set('config', new Zend_Config($this->getOptions()));
	}

	public function run()
	{
		// make the config available to everyone


		parent::run();
	}

	protected function _initAutoload()
	{
		$loader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH));

		return $loader;
	}

	protected function _initSession()
	{
		$config = Zend_Registry::get('config');
		$default = $config->default->session->name;

		$session = new Zend_Session_Namespace($default, true);

		return $session;
	}
	
	protected function _initRegistry()
	{
	    //$db = Zend_Db_Table::getDefaultAdapter();
		
		  $resource = $this->getPluginResource('db');
	
	    $db = $resource->getDbAdapter();
	    $db->query(new Zend_Db_Expr('SET NAMES utf8'));
	    $db->setFetchMode ( Zend_Db::FETCH_ASSOC );
	    
	    $db->getProfiler()->setEnabled(true);
	
	    Zend_Registry::set("db", $db);
	}

	public function _initLocale()
	{
		$locale  = new Zend_Locale('en_US');
		 
		Zend_Registry::set('Zend_Locale', $locale);

		$region = $locale->getRegion();
		$lang = $locale->getLanguage();

		$translationFile = APPLICATION_PATH . "/languages/$lang.php";
		$translate = new Zend_Translate('array', $translationFile, $lang);
		Zend_Registry::set('Zend_Translate', $translate);
	}

	protected function _initView()
	{
		$view = new Zend_View();
		$view->doctype('XHTML1_STRICT');

		$view->addHelperPath(APPLICATION_PATH . '/views/helpers');

		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);

		$acl = App_AccessList::getAcl();
	
		$session = $this->getResource('session');
		$storage = new App_Auth_Storage_Session($session, 'client');
		if (!$storage->isEmpty()){
			
			$client_id = $storage->getUserId();
			
			$view->user_id = $client_id; 
			$view->user_name = $storage->getUserName();
			$view->user_role = $storage->getUserRole();
			//Zend_Debug::dump($storage->getUserId());
			
			$model_link = new Model_ServiceCentral();
			$accounts = $model_link->linkedAccounts($client_id);
			//Zend_Debug::dump($accounts);
			//Zend_Registry::set('accounts', $accounts);
			
			$view->user_accounts = $accounts[$client_id];
			//Zend_Debug::dump($accounts[$client_id]);exit;
		}
				
		$storage = new App_Auth_Storage_Session($session, 'admin');
	    if (!$storage->isEmpty()){
	      $view->admin_user_id = $storage->getUserId();
	      $view->admin_user_name = $storage->getUserName();
	      //Zend_Debug::dump($storage->getUserId());
	    }
		
		
		return $view;
	}
	
	
	

}

